//
//  CNBBSDetailViewModel.m
//  CsdnPlus
//
//  Created by 孟瑞瑞 on 2018/7/18.
//  Copyright © 2018年 CSDN. All rights reserved.
//

#import "CNBBSDetailViewModel.h"
#import <CNBaseTool/CSDNTool.h>
#import <CNBaseViewTool/CSDNUser.h>
@implementation CNBBSDetailViewModel
/**
 论坛详情数据
 
 @param topicId 论坛id
 
 @return 网络请求任务
 */
-(void)getBBSDetailWithTopicId:(NSString *)topicId CompletionHandle:(CompletionHandle)completionHandle
{
    self.dataTask = [CNBBSNetManager getBBSDetailWithTopicId:topicId completionHandle:^(CNBBSDetailModel *model, NSError *error) {
        
        if (!error) {
            self.code = model.code;
            self.message = model.msg;
            if (self.code == 200) {
                
                if (!model.data.nickname || [model.data.nickname isEqualToString:@""])
                {
                    self.nickname = model.data.username;
                }
                else
                {
                    self.nickname = model.data.nickname;
                }
                
                
                self.digg_count = model.data.digg_count;
                
                self.url = model.data.url;
                
                self.score = model.data.score;
                
                self.is_digged = model.data.is_digged;
                
                self.is_fav = model.data.is_fav;
                
                self.text_body = model.data.text_body;
                
                self.level = model.data.level;
                
                if ([model.data.avatar hasPrefix:@"//"])
                {
                    self.avatar = [NSString stringWithFormat:@"%@%@",@"https:",model.data.avatar];
                }
                else
                {
                    self.avatar = [model.data.avatar stringByReplacingOccurrencesOfString:@"http:" withString:@"https:"];
                }
                
                self.created_at = [CSDNTool compareCurrentTime:model.data.created_at];
                
                self.username = model.data.username;
                
                self.title = model.data.title;
                
                self.desc = model.data.introduction;
                
                self.postId = model.data.post_id;
            }
            
        }
        completionHandle(error);
    }];
}




/**
 获取文章或者论坛是否收藏状态
 
 @param url 文章地址
 
 @return 网络请求任务
 */
-(void)getCheckCollectionWithUrl:(NSString *)url CompletionHandle:(CompletionHandle)completionHandle
{
    self.dataTask = [CNBBSNetManager getCheckCollectionWithUrl:url completionHandle:^(CNFavModel *model, NSError *error) {
        if (!error) {
            self.code = model.code;
            self.message = model.msg;
            if (self.code == 200)
            {
                self.is_Fav = model.data.isExist;
                self.favorite_id = model.data.favoriteId;
            }
            else if (self.code == 401)
            {
                //未登录
                [[CSDNUser currentUser] reset];
                
            }
            
        }
        completionHandle(error);
    }];
}
@end
